<template>
  <el-dialog
    v-model="visible"
    v-if="visible"
    :title="props.options.title"
    width="40%"
    :before-close="handleClose"
  >
    <el-form ref="formRef" :model="formData" label-width="120px" class="form-container">
      <el-row :gutter="20">
        <el-col :span="12">
          <el-form-item label="创建人" prop="creator">
            <el-input disabled v-model="formData.creator" placeholder="请输入创建人" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="来源日期" prop="sourceDate">
            <!-- <el-date-picker style="width: 100%;" type="date" value-format="YYYY-MM-DD" v-model="formData.sourceDate"
              placeholder="请选择来源日期"></el-date-picker> -->
            <el-date-picker
              v-model="formData.sourceDate"
              style="width: 100%"
              type="date"
              placeholder="请选择来源日期"
            />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="办单日期" prop="handleDate">
            <el-date-picker
              v-model="formData.handleDate"
              style="width: 100%"
              type="date"
              placeholder="请选择办单日期"
            />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="来源" prop="source">
            <el-input v-model="formData.source" placeholder="请输入来源"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="回款金额" prop="paymentAmount">
            <el-input v-model="formData.paymentAmount" placeholder="请输入回款金额"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="垫付金额" prop="advanceAmount">
            <el-input v-model="formData.advanceAmount" placeholder="请输入垫付金额"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="企业号" prop="enterpriseNo">
            <el-input v-model="formData.enterpriseNo" placeholder="请输入企业号"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="平台" prop="platform">
            <el-select v-model="formData.platform" placeholder="请选择平台">
              <el-option
                v-for="item in getIntDictOptions(DICT_TYPE.PLATFORM)"
                :label="item.label"
                :value="item.value"
              />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="手机号码" prop="customerPhone">
            <el-input v-model="formData.customerPhone" placeholder="请输入手机号码"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="客户名称" prop="customerName">
            <el-input v-model="formData.customerName" placeholder="请输入客户名称"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="客户年龄" prop="customerAge">
            <el-input v-model="formData.customerAge" placeholder="请输入客户年龄"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="芝麻分" prop="customerCreditScore">
            <el-input
              v-model.number="formData.customerCreditScore"
              placeholder="请输入芝麻分"
              type="number"
            ></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="型号颜色" prop="modelColor">
            <el-input v-model="formData.modelColor" placeholder="请输入型号颜色"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="第几台" prop="deviceCount">
            <el-input
              v-model.number="formData.deviceCount"
              placeholder="请输入第几台"
              type="number"
            ></el-input>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <div class="modal-footer">
      <el-button @click="handleClose">取消</el-button>
      <el-button type="primary" @click="handleSubmit">提交</el-button>
    </div>
  </el-dialog>
</template>

<script setup>
import { ref, defineEmits, watch, defineProps, reactive } from 'vue'
import * as ticketManagementApi from '@/api/ticketManagement/index'
import { ElMessage } from 'element-plus'
import { DICT_TYPE, getIntDictOptions } from '@/utils/dict'
const props = defineProps({
  type: {
    type: String,
    default: 'add'
  },
  options: {
    type: Object,
    default: {}
  }
})
const userData = ref()
onMounted(() => {})
const title = ref('')
// 定义表单初始数据
const initFormData = {}

// 表单数据
const formData = ref({
  sourceDate: '',
  handleDate: ''
})
// 表单引用
const formRef = ref(null)
// 弹窗可见性
const visible = ref(false)
// 定义事件
const emit = defineEmits(['onClose', 'onSubmit'])
const getNowDate = () => {
  const date = new Date()
  // 获取年份（4位）
  const year = date.getFullYear()
  const month = date.getMonth()
  const day = date.getDate()
  return `${year}-${month + 1}-${day}`
}

// 打开弹窗方法
const onOpen = (data) => {
  console.log('data', data)
  if (data.id) {
    formData.value = JSON.parse(JSON.stringify(data))
  } else {
    formData.value = {}
    nextTick(() => {
      userData.value = JSON.parse(JSON.parse(localStorage.getItem('user')).v)
      formData.value.creator = userData.value.user.nickname
      formData.value.deptId = userData.value.user.deptId
      const date = getNowDate()
      formData.value.sourceDate = String(date)
      formData.value.handleDate = String(date)
    })
  }
  nextTick(() => {
    visible.value = true
  })
}

// 关闭弹窗
const handleClose = () => {
  visible.value = false
  nextTick(() => {
    emit('onClose')
  })
}

// 提交表单
const handleSubmit = () => {
  console.log('props.options.type', props.options.type)
  if (props.options.type == 'add') {
    formData.value.status = 1
    ticketManagementApi.addWorkorder(formData.value).then((res) => {
      ElMessage.success('添加成功')
    })
  } else if (props.options.type == 'edit') {
    ticketManagementApi.editWorkorder(formData.value).then((res) => {
      ElMessage.success('修改成功')
    })
  }
  setTimeout(() => {
    emit('onSubmit')
  }, 500)

  handleClose()
}

// 暴露打开弹窗的方法
defineExpose({ onOpen })
</script>

<style scoped>
.modal-overlay {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background-color: rgba(0, 0, 0, 0.5);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 1000;
}

.modal-container {
  width: 40%;
  max-width: 1200px;
  background-color: #fff;
  border-radius: 8px;
  box-shadow: 0 2px 12px rgba(0, 0, 0, 0.1);
  max-height: 90vh;
  display: flex;
  flex-direction: column;
}

.modal-header {
  padding: 16px 20px;
  border-bottom: 1px solid #e5e7eb;
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.modal-header h3 {
  margin: 0;
  font-size: 18px;
  font-weight: 500;
}

.close-btn {
  background: none;
  border: none;
  font-size: 20px;
  cursor: pointer;
  color: #666;
}

.modal-body {
  padding: 20px;
  overflow-y: auto;
  flex: 1;
}

.form-container {
  width: 100%;
}

.modal-footer {
  padding: 16px 20px;
  border-top: 1px solid #e5e7eb;
  display: flex;
  justify-content: flex-end;
  gap: 10px;
}
</style>
